/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Admin_SearchStore.java
 *
 * Created on Mar 31, 2011, 1:48:25 AM
 */
package admin_gui;

import admin.Controller.StoreController;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author edaype
 */
public class Admin_SearchStore extends javax.swing.JPanel {

    private String storeName;
    private Vector v_head;
    // private JPanel mainPanel;
    private StoreController storeController;
    private String searchField;
    private boolean[] storeItem;

    public Admin_SearchStore(JPanel mainPanel) {
        initComponents();
        //  this.mainPanel = mainPanel;
        v_head = new Vector();
        //     v_head.add("storeID");        No store ID?
        v_head.add("Name");
        v_head.add("Phone");
        v_head.add("Street Number");
        v_head.add("Street");
        v_head.add("Postal Code");
        v_head.add("City");
        v_head.add("Province");
        searchField = null;
        storeController = new StoreController(mainPanel);
        storeItem = new boolean[6];
        storeItem[0] = true;
        for (int i = 1; i < 6; i++) {
            storeItem[i] = false;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TopPanel = new javax.swing.JPanel();
        SearchLabel = new javax.swing.JLabel();
        SearchField = new javax.swing.JTextField();
        OptionSearch = new javax.swing.JComboBox();
        GoButton = new javax.swing.JButton();
        ByLabel = new javax.swing.JLabel();
        ResultPane = new javax.swing.JScrollPane();
        ResultTable = new javax.swing.JTable();
        ButtonPanel = new javax.swing.JPanel();
        SearchTitle = new javax.swing.JLabel();
        EditButton = new javax.swing.JButton();
        DeleteButton = new javax.swing.JButton();
        InstructionsLabel = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        TopPanel.setBackground(new java.awt.Color(255, 255, 255));
        TopPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        SearchLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        SearchLabel.setText("Search");

        SearchField.setFont(new java.awt.Font("Tahoma", 0, 24));

        OptionSearch.setFont(new java.awt.Font("Tahoma", 0, 24));
        OptionSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select--", "Name", "Phone", "Post Code", "City", "Province" }));
        OptionSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OptionSearchActionPerformed(evt);
            }
        });

        GoButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        GoButton.setText("Go");
        GoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GoButtonActionPerformed(evt);
            }
        });

        ByLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        ByLabel.setText(" By");

        javax.swing.GroupLayout TopPanelLayout = new javax.swing.GroupLayout(TopPanel);
        TopPanel.setLayout(TopPanelLayout);
        TopPanelLayout.setHorizontalGroup(
            TopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(TopPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(SearchLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SearchField, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(48, 48, 48)
                .addComponent(ByLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(OptionSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 167, Short.MAX_VALUE)
                .addComponent(GoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        TopPanelLayout.setVerticalGroup(
            TopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(TopPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(TopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SearchLabel)
                    .addComponent(SearchField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(GoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ByLabel)
                    .addComponent(OptionSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        ResultTable.setAutoCreateRowSorter(true);
        ResultTable.setFont(new java.awt.Font("Tahoma", 0, 12));
        ResultTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "storeID", "Name", "Phone", "Street Number", "Street", "Postal Code", "City", "Province"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        ResultTable.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        ResultTable.setGridColor(new java.awt.Color(0, 0, 0));
        ResultTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        ResultTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                ResultTableMousePressed(evt);
            }
        });
        ResultPane.setViewportView(ResultTable);
        ResultTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        ButtonPanel.setBackground(new java.awt.Color(255, 255, 255));

        SearchTitle.setFont(new java.awt.Font("Copperplate Gothic Bold", 3, 48));
        SearchTitle.setText("Search Store");

        EditButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        EditButton.setText("Edit");
        EditButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditButtonActionPerformed(evt);
            }
        });

        DeleteButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        DeleteButton.setText("Delete");
        DeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ButtonPanelLayout = new javax.swing.GroupLayout(ButtonPanel);
        ButtonPanel.setLayout(ButtonPanelLayout);
        ButtonPanelLayout.setHorizontalGroup(
            ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(SearchTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 628, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(EditButton, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DeleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        ButtonPanelLayout.setVerticalGroup(
            ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonPanelLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(EditButton)
                .addGap(18, 18, 18)
                .addComponent(DeleteButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ButtonPanelLayout.createSequentialGroup()
                .addContainerGap(37, Short.MAX_VALUE)
                .addComponent(SearchTitle)
                .addGap(24, 24, 24))
        );

        InstructionsLabel.setText("Please select an item and click button to take action");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ResultPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 795, Short.MAX_VALUE)
                    .addComponent(TopPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(InstructionsLabel)
                    .addComponent(ButtonPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(TopPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(InstructionsLabel)
                .addGap(11, 11, 11)
                .addComponent(ResultPane, javax.swing.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(ButtonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void GoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GoButtonActionPerformed
        searchField = SearchField.getText();
        //    System.out.print("in Store.go: "+searchField);
        storeController.manageStore(ResultTable, v_head, searchField, storeItem);
    }//GEN-LAST:event_GoButtonActionPerformed

    private void ResultTableMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ResultTableMousePressed
        //     int column = ResultTable.getColumnModel().getColumnIndexAtX(evt.getX());
        int row = evt.getY() / ResultTable.getRowHeight();
        String result = (ResultTable.getValueAt(row, 0)).toString().trim();
        storeName = result;     //storeID here is in fact the store name
        //  System.out.println("storeName: " + result);
    }//GEN-LAST:event_ResultTableMousePressed

    private void EditButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditButtonActionPerformed
        // TODO add your handling code here:
        if (storeName == null) {
            //    System.out.println("storeName: " + storeName);
            JOptionPane.showMessageDialog(null, "Please select one record",
                    "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            storeController.showEditStorePanel(storeName);
        }
    }//GEN-LAST:event_EditButtonActionPerformed

    private void OptionSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OptionSearchActionPerformed
        // TODO add your handling code here:
        JComboBox cb = (JComboBox) evt.getSource();
        String petName = (String) cb.getSelectedItem();
        //  System.out.println("Select Store " + petName);
        int count = cb.getSelectedIndex();
        storeItem[count] = true;
        for (int i = 0; i < 6; i++) {
            if (i != count) {
                storeItem[i] = false;
            }
        }
    }//GEN-LAST:event_OptionSearchActionPerformed

    private void DeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteButtonActionPerformed
        // TODO add your handling code here:
        //   System.out.println("store: " + storeName);
        if (storeName == null) {
            //    System.out.println("storeName: " + storeName);
            JOptionPane.showMessageDialog(null, "Please select one record",
                    "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            storeController.deleteStore(storeName, ResultTable, v_head);
        }
    }//GEN-LAST:event_DeleteButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel ButtonPanel;
    private javax.swing.JLabel ByLabel;
    private javax.swing.JButton DeleteButton;
    private javax.swing.JButton EditButton;
    private javax.swing.JButton GoButton;
    private javax.swing.JLabel InstructionsLabel;
    private javax.swing.JComboBox OptionSearch;
    private javax.swing.JScrollPane ResultPane;
    private javax.swing.JTable ResultTable;
    private javax.swing.JTextField SearchField;
    private javax.swing.JLabel SearchLabel;
    private javax.swing.JLabel SearchTitle;
    private javax.swing.JPanel TopPanel;
    // End of variables declaration//GEN-END:variables
}
